package com.rongshu.view.userInfo.presenter;

import android.content.Context;

import com.rongshu.api.RetorfitService;
import com.rongshu.utils.Common;
import com.rongshu.utils.UserInfoUtils;
import com.rongshu.view.base.mvp.BasePresenter;
import com.rongshu.view.penpal.bean.PenpalResponse;
import com.rongshu.view.userInfo.iview.PenpalWillView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import rx.Subscriber;
import rx.functions.Func1;

public class PenpalWillPresenter extends BasePresenter<PenpalWillView> {

    public PenpalWillPresenter(Context context) {
        super(context);
    }

    /**
     * 我的所有笔友会
     */
    public void getJoinedPenpal(int pageIndex, int pageSize) {
        //http
        Map<String, Object> map = new HashMap<>();
        map.put("user_id", UserInfoUtils.getUserId());
        map.put("pageIndex", pageIndex);
        map.put("pageSize", pageSize);
        RetorfitService.getJoinedPenpal(UserInfoUtils.getUserId(), pageIndex, pageSize, Common.encryptionSign(map))
                .map(new Func1<PenpalResponse, List<PenpalResponse.ResponseBean>>() {
                    @Override
                    public List<PenpalResponse.ResponseBean> call(PenpalResponse response) {
                        return response.getResponse();
                    }
                })
                .subscribe(new Subscriber<List<PenpalResponse.ResponseBean>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(List<PenpalResponse.ResponseBean> responseBeans) {
                        if (getView() != null) {
                            getView().onPenpalWillMsg(responseBeans);
                        }
                    }
                });
    }

    /**
     * Ta的所有笔友会
     */
    public void getTaJoinedPenpal(int taId, int pageIndex, int pageSize) {
        //http
        Map<String, Object> map = new HashMap<>();
        map.put("user_id", UserInfoUtils.getUserId());
        map.put("ta_user_id", taId);
        map.put("pageIndex", pageIndex);
        map.put("pageSize", pageSize);
        RetorfitService.getTaJoinedPenpal(UserInfoUtils.getUserId(), taId, pageIndex, pageSize, Common.encryptionSign(map))
                .map(new Func1<PenpalResponse, List<PenpalResponse.ResponseBean>>() {
                    @Override
                    public List<PenpalResponse.ResponseBean> call(PenpalResponse response) {
                        return response.getResponse();
                    }
                })
                .subscribe(new Subscriber<List<PenpalResponse.ResponseBean>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(List<PenpalResponse.ResponseBean> responseBeans) {
                        if (getView() != null) {
                            getView().onPenpalWillMsg(responseBeans);
                        }
                    }
                });
    }
}
